package com.hm.health.widget;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hm.health.R;
import com.hm.health.adapter.CheckBoxPropertyAdapter;
import com.hm.health.entity.CheckBoxProperty;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenshi on 2019/12/29.
 */

public class CheckBoxPopWindow {

    public interface Callback {
        void onCheckBoxPropertyChanged(boolean enableFilter, List<CheckBoxProperty> properties);
    }

    private Activity mContext;
    private PopupWindow mPopWindow;
    private RecyclerView mRecyclerView;
    private List<CheckBoxProperty> mCheckBoxProperties = new ArrayList<>();
    private CheckBoxPropertyAdapter mCheckBoxPropertyAdapter;
    private View mRootView;
    private CheckBox mCbFilter;

    public CheckBoxPopWindow(Activity context, View view) {
        mContext = context;
        mRootView = LayoutInflater.from(mContext).inflate(R.layout.pop_window_check_box, null);
        initPopWindow(mRootView, view, Gravity.BOTTOM);
    }

    public void show(boolean enableFilter, List<CheckBoxProperty> properties, Callback callback) {
        mCheckBoxProperties.clear();
        mRecyclerView = mRootView.findViewById(R.id.rv_measure_results);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setHasFixedSize(true);
        for (CheckBoxProperty p : properties) {
            CheckBoxProperty p1 = new CheckBoxProperty(p.getColorChecked(), p.getColorUnchecked(), p.getText(), p.isChecked(), p.getChannelId());
            mCheckBoxProperties.add(p1);
        }
        mCheckBoxPropertyAdapter = new CheckBoxPropertyAdapter(mContext, mCheckBoxProperties);
        mRecyclerView.setAdapter(mCheckBoxPropertyAdapter);

        mCbFilter = mRootView.findViewById(R.id.cb_base_filter);
        mCbFilter.setChecked(enableFilter);

        mRootView.findViewById(R.id.tv_ok).setOnClickListener(view -> {
            if (null != callback) {
                boolean enable = mCbFilter.isChecked();
                callback.onCheckBoxPropertyChanged(enable, mCheckBoxProperties);
                mPopWindow.dismiss();
            }
        });

        mRootView.findViewById(R.id.tv_cancel).setOnClickListener(view -> mPopWindow.dismiss());



    }

    private void initPopWindow(View popView, View view, int gravity) {
        mPopWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        setBackgroundAlpha(0.5f);
        mPopWindow.setTouchable(true);
        ColorDrawable colorDrawable = new ColorDrawable(mContext.getResources().getColor(R.color.popup_main_background));
        mPopWindow.setBackgroundDrawable(colorDrawable);
        mPopWindow.setTouchInterceptor((v, event) -> false);
        mPopWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.color.white));
        mPopWindow.showAtLocation(view, gravity, 0, 0);
        mPopWindow.setOnDismissListener(() -> setBackgroundAlpha(1f));
    }

    private void setBackgroundAlpha(float fAlpha) {
        WindowManager.LayoutParams layoutParams = mContext.getWindow().getAttributes();
        layoutParams.alpha = fAlpha;
        mContext.getWindow().setAttributes(layoutParams);
    }
}
